﻿// 功能：ImagingHelper封装
// 描述：
// 作者：温伟鹏
// 日期：2010/6/2 14:19:03

using System;
using System.Runtime.InteropServices;
using System.IO;
using System.Drawing;

namespace MeizuSDK.Drawing
{
    /// <summary>
    /// ImagingHelper封装
    /// </summary>
    public class ImagingHelper : IDisposable
    {
        #region DllImport
        [DllImport("MZFCWrapper.dll")]
        private static extern IntPtr ImagingHelperCtor();

        [DllImport("MZFCWrapper.dll")]
        private static extern bool ImagingHelperLoadImage(IntPtr pObj, string filename, bool bCopyToInternalDC, bool bUnloadAfterCopy, bool bUseAlpha);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool ImagingHelperLoadImageFromRes(IntPtr pObj, IntPtr hInst, int nResId, bool bCopyToInternalDC, bool bUnloadAfterCopy, bool bUseAlpha);

        [DllImport("MZFCWrapper.dll")]
        private static extern void ImagingHelperDraw(IntPtr pObj, IntPtr hdcDst, ref RECT prcDst, bool bStretch, bool bStretchScale);

        [DllImport("MZFCWrapper.dll")]
        private static extern int ImagingHelperGetImageWidth(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern int ImagingHelperGetImageHeight(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool ImagingHelperIsImageAlpha(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void ImagingHelperUnload(IntPtr pObj);
        #endregion

        #region 私有变量
        private IntPtr handle;
        private bool imgLoaded;
        private IntPtr hInst;
        #endregion

        #region 公共属性
        /// <summary>
        /// 获取ImagingHelper句柄
        /// </summary>
        public IntPtr Handle
        {
            get
            {
                return this.handle;
            }
            protected set
            {
                this.handle = value;
            }
        }
        /// <summary>
        /// 获取已加载的图像宽度（如果未加载图片，则返回-1）
        /// </summary>
        public int ImageWidth
        {
            get
            {
                if (!imgLoaded)
                    return -1;

                return ImagingHelperGetImageWidth(Handle);
            }
        }
        /// <summary>
        /// 获取已加载的图像高度（如果未加载图片，则返回-1）
        /// </summary>
        public int ImageHeight
        {
            get
            {
                if (!imgLoaded)
                    return -1;

                return ImagingHelperGetImageHeight(Handle);
            }
        }
        #endregion

        #region 构造函数
        /// <summary>
        /// 构造ImagingHelper
        /// </summary>
        public ImagingHelper()
        {
            Handle = ImagingHelperCtor();
            imgLoaded = false;
        }
        /// <summary>
        /// ImageContainer类调用
        /// </summary>
        /// <param name="pObj"></param>
        internal ImagingHelper(IntPtr pObj)
        {
            Handle = pObj;
            imgLoaded = true;
        }
        #endregion

        #region 函数
        /// <summary>
        /// 从指定文件加载图像
        /// </summary>
        /// <param name="fileName">图像文件路径</param>
        public void LoadImage(string fileName)
        {
            if (!File.Exists(fileName))
            {
                throw new FileNotFoundException();
            }

            ImagingHelperLoadImage(Handle, fileName, true, true, true);

            imgLoaded = true;
        }
        /// <summary>
        /// 从MzResV2.dll中加载图像
        /// </summary>
        /// <param name="resId">资源Id</param>
        /// <param name="useAlpha">是否使用图像中的Alpha通道</param>
        public void LoadImageFromResV2(MzResV2 resId, bool useAlpha)
        {
            LoadImageFromResV2((int)resId, useAlpha);
        }
        /// <summary>
        /// 从MzResV2.dll中加载图像
        /// </summary>
        /// <param name="resId">资源Id</param>
        /// <param name="useAlpha">是否使用图像中的Alpha通道</param>
        public void LoadImageFromResV2(int resId, bool useAlpha)
        {
            hInst = MzGraphicsAPI.GetMzResV2ModuleHandle();

            LoadImageFromRes(hInst, resId, useAlpha);
        }
        /// <summary>
        /// 从资源中加载图像
        /// </summary>
        /// <param name="hInstance">资源模块句柄</param>
        /// <param name="resId">资源ID</param>
        /// <param name="useAlpha">是否使用图像中的通道</param>
        public void LoadImageFromRes(IntPtr hInstance, int resId, bool useAlpha)
        {
            ImagingHelperLoadImageFromRes(Handle, hInstance, resId, true, true, useAlpha);

            imgLoaded = true;
        }
        /// <summary>
        /// 将加载的图像绘制到指定绘图面上
        /// </summary>
        /// <param name="g">绘图图面</param>
        /// <param name="prcDst">目的矩形</param>
        /// <param name="stretch">是否拉伸</param>
        /// <param name="stretchScale">是否按比例拉伸</param>
        public void Draw(Graphics g, Rectangle prcDst, bool stretch, bool stretchScale)
        {
            if (imgLoaded)
            {
                IntPtr hdc = g.GetHdc();

                RECT rect = RECT.FromRectangle(prcDst);

                ImagingHelperDraw(Handle, hdc, ref rect, stretch, stretchScale);

                g.ReleaseHdc(hdc);
            }
        }
        /// <summary>
        /// 释放已加载的图像资源
        /// </summary>
        public void Unload()
        {
            if(hInst!=IntPtr.Zero)
            {
                MzGraphicsAPI.FreeMzResV2Module();
                MzGraphicsAPI.FreeMzResModule();
            }

            if(Handle!=IntPtr.Zero)
            {
                //ImagingHelperUnload(Handle);
            }
        }
        #endregion

        #region IDisposable 成员
        /// <summary>
        /// 销毁资源
        /// </summary>
        public void Dispose()
        {
            Unload();
        }

        #endregion
    }
}
